package com.mofofa.maozy.network

import androidx.lifecycle.ViewModel
import com.mofofa.maozy.data.*
import com.mofofa.maozy.utils.InfoProcessUtil
import com.google.gson.JsonObject
import com.mofofa.maozy.utils.AppConfigUtil
import com.mofofa.maozy.utils.UserStateUtil
import com.mofofa.maozy.utils.analytics.event.IAnalyticsEvent
import com.mofofa.maozy.utils.analytics.util.AnalyticsUtil
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

object ApiUserReposity : ApiBaseReposity() {

    /** 获取启动配置*/
    fun fetchMaozyGlobalMaozyConfig(
        caller: ViewModel?,
        reqZone: ReqZone
    ) =
        fire(Dispatchers.IO, caller, reqZone.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyGlobalMaozyConfig(reqZone)
            if (response.code.equals("0000", true) or response.code.equals("0", true)) {
                val obj = response.obj
                AppConfigUtil.mMaozyGlobalMaozyConfig = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyAppMaozyConfig(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyAppMaozyConfig(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                AppConfigUtil.mMaozySysMaozyConfig = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyContactMaozyList(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyAppMaozyConfig(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                AppConfigUtil.mMaozySysMaozyConfig = obj
                Result.success(obj.customize.getMaozyContactMaozyList())
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyAppMaozyVersion(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyAppMaozyVersion(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyOtp(caller: ViewModel?, reqDoLogin: ReqDoLogin) =
        fire(Dispatchers.IO, caller, reqDoLogin.showLoading) {
            val response = ApiUserNetwork.fetchMaozyOtp(reqDoLogin)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                UserStateUtil.saveMaozyMobile(reqDoLogin.mobile)
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun doMaozyLogin(caller: ViewModel?, reqDoLogin: ReqDoLogin) =
        fire(Dispatchers.IO, caller, reqDoLogin.showLoading) {
            val response = ApiUserNetwork.doMaozyLogin(reqDoLogin)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                obj.mobile = reqDoLogin.mobile
                //更新登录信息
                UserStateUtil.updateMaozyLoginMaozyState(obj)

                //推送注册事件
                if (obj.isMaozyNewMaozyUser()) {
                    AnalyticsUtil.addEvent(IAnalyticsEvent.AT_REGISTER)
                }

                //保存s2s信息
                AnalyticsUtil.saveFirebaseAndFacebookS2SInfo()

                //回调
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyUserMaozyState(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyUserMaozyState(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                InfoProcessUtil.mMaozyUserMaozyProcess = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyIsMaozyRepayMaozyNow(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyUserMaozyState(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                InfoProcessUtil.mMaozyUserMaozyProcess = obj
                if (InfoProcessUtil.isMaozyRepayMaozyNow()) {
                    Result.success(true)
                } else {
                    Result.success(false)
                }
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyCanMaozyCreateMaozyLoan(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyUserMaozyState(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                InfoProcessUtil.mMaozyUserMaozyProcess = obj
                if (InfoProcessUtil.isMaozyRepayMaozyNow() or
                    InfoProcessUtil.isMaozyLoaningMaozyNow() or
                    InfoProcessUtil.isMaozyReviewingMaozyNow() or
                    InfoProcessUtil.isMaozyRejectMaozyNow()
                ) {
                    Result.success(false)
                } else {
                    Result.success(true)
                }
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyRepayMaozyUrlMaozyGHA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchMaozyRepayMaozyUrlMaozyGHA(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyRepayMaozyUrlMaozyUGX(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchMaozyRepayMaozyUrlMaozyUGX(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyRepayMaozyUrlMaozyKSH(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchMaozyRepayMaozyUrlMaozyKSH(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyRepayMaozyUrlMaozyFCFA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchMaozyRepayMaozyUrlMaozyFCFA(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyRepayMaozyUrlMaozyVND(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.fetchMaozyRepayMaozyUrlMaozyVND(
                reqBase
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyHomeMaozyCredit(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyHomeMaozyCredit(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyLoaningMaozyDetail(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyLoaningMaozyDetail(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyRepayMaozyDetail(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyRepayMaozyDetail(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyFeedback(
        caller: ViewModel?,
        reqSaveFeedback: ReqSaveFeedback
    ) =
        fire(Dispatchers.IO, caller, reqSaveFeedback.showLoading) {
            val response = ApiUserNetwork.saveMaozyFeedback(reqSaveFeedback)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyInfo(caller: ViewModel?, targetUrl: String, jsonObject: JsonObject) =
        fire(Dispatchers.IO, caller, true) {
            val response = ApiUserNetwork.saveMaozyInfo(targetUrl, jsonObject)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyInfoMaozyOcr(
        caller: ViewModel?,
        reqSaveOcrResultVnd: ReqSaveOcrResultVnd
    ) =
        fire(Dispatchers.IO, caller, reqSaveOcrResultVnd.showLoading) {
            val response = ApiUserNetwork.saveMaozyInfoMaozyOcr(
                reqSaveOcrResultVnd
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyInfoMaozyLiveness(
        caller: ViewModel?,
        reqSaveLivenessVnd: ReqSaveLivenessVnd
    ) =
        fire(Dispatchers.IO, caller, reqSaveLivenessVnd.showLoading) {
            val response = ApiUserNetwork.saveMaozyInfoMaozyLiveness(
                reqSaveLivenessVnd
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun startMaozyLiveness(
        caller: ViewModel?,
        reqSaveLivenessVnd: ReqSaveLivenessVnd
    ) =
        fire(Dispatchers.IO, caller, reqSaveLivenessVnd.showLoading) {
            val response = ApiUserNetwork.startMaozyLiveness(
                reqSaveLivenessVnd
            )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyBankMaozyCodeMaozyList(
        caller: ViewModel?,
        reqGetBankList: ReqGetBankList
    ) =
        fire(Dispatchers.IO, caller, reqGetBankList.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyBankMaozyCodeMaozyList(
                    reqGetBankList
                )
            if (response.code.equals("0000", true)) {
                response.uITag = reqGetBankList.uITag
                Result.success(response)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun createMaozyLoan(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response = ApiUserNetwork.createMaozyLoan(reqBase)
            if (response.code.equals("0000", true)) {
                AnalyticsUtil.addEvent(IAnalyticsEvent.AT_LOAN_APPLY)
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun checkMaozyAppMaozyReview(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.checkMaozyAppMaozyReview(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyVirtualMaozyAccount(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyVirtualMaozyAccount(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun initMaozyPaystackMaozySdk(
        caller: ViewModel?,
        reqPaystackOrderInitNgn: ReqPaystackOrderInitNgn
    ) =
        fire(Dispatchers.IO, caller, reqPaystackOrderInitNgn.showLoading) {
            val response =
                ApiUserNetwork.initMaozyPaystackMaozySdk(reqPaystackOrderInitNgn)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun fetchMaozyPaystackMaozyCardMaozyList(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.fetchMaozyPaystackMaozyCardMaozyList(
                    reqBase
                )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getMaozyWalletMaozyListMaozyFCFA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getMaozyWalletMaozyListMaozyFCFA(
                    reqBase
                )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyWalletMaozyFCFA(
        caller: ViewModel?,
        reqSaveWalletFcFa: ReqSaveWalletFcFa
    ) =
        fire(Dispatchers.IO, caller, reqSaveWalletFcFa.showLoading) {
            val response =
                ApiUserNetwork.saveMaozyWalletMaozyFCFA(reqSaveWalletFcFa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun deleteMaozyWalletMaozyFCFA(
        caller: ViewModel?,
        reqDeleteWalletFcfa: ReqDeleteWalletFcfa
    ) =
        fire(Dispatchers.IO, caller, reqDeleteWalletFcfa.showLoading) {
            val response =
                ApiUserNetwork.deleteMaozyWalletMaozyFCFA(reqDeleteWalletFcfa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyBvnMaozyFCFA(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.saveMaozyBvnMaozyFCFA(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getMaozyWalletMaozyListMaozyXaf(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getMaozyWalletMaozyListMaozyXaf(
                    reqBase
                )
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyWalletMaozyXaf(
        caller: ViewModel?,
        reqSaveWalletFcFa: ReqSaveWalletFcFa
    ) =
        fire(Dispatchers.IO, caller, reqSaveWalletFcFa.showLoading) {
            val response =
                ApiUserNetwork.saveMaozyWalletMaozyXaf(reqSaveWalletFcFa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun deleteMaozyWalletMaozyXaf(
        caller: ViewModel?,
        reqDeleteWalletFcfa: ReqDeleteWalletFcfa
    ) =
        fire(Dispatchers.IO, caller, reqDeleteWalletFcfa.showLoading) {
            val response =
                ApiUserNetwork.deleteMaozyWalletMaozyXaf(reqDeleteWalletFcfa)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyBvnMaozyXaf(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.saveMaozyBvnMaozyXaf(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun uploadMaozyImage(
        caller: ViewModel?,
        reqImageUpload: ReqImageUpload
    ) =
        fire(Dispatchers.IO, caller, reqImageUpload.showLoading) {
            val map: Map<String, String> = HashMap()
            val file = File(reqImageUpload.filePath)

            val requestBody =
                RequestBody.create(reqImageUpload.mediaType.toMediaTypeOrNull(), file)
            val bodyPart: MultipartBody.Part =
                MultipartBody.Part.createFormData("image", file.name, requestBody)

            val response =
                ApiUserNetwork.uploadMaozyImage(map, bodyPart)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                obj.type = reqImageUpload.cardType
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun startMaozyOcr(
        caller: ViewModel?,
        reqDoOcrVnd: ReqDoOcrVnd
    ) =
        fire(Dispatchers.IO, caller, reqDoOcrVnd.showLoading) {
            val response =
                ApiUserNetwork.startMaozyOcr(reqDoOcrVnd)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun startMaozySim(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.startMaozySim(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun saveMaozyInfoMaozySim(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.saveMaozyInfoMaozySim(reqBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}